package com.fick.hibernate.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.fick.hibernate.model.Mediaclass;

/**
 * A data access object (DAO) providing persistence and search support for
 * Mediaclass entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.fick.Mediaclass.nrgl.Mediaclass
 * @author MyEclipse Persistence Tools
 */

public class MediaclassDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(MediaclassDAO.class);
	// property constants
	public static final String MEDIACLASS_DESCRIPTION = "mediaclassDescription";
	public static final String MEDIACLASS_ORDER = "mediaclassOrder";
	public static final String MEDIACLASS_STATUS = "mediaclassStatus";
	public static final String ADDUSER = "adduser";

	public void attachClean(Mediaclass instance) {
		log.debug("attaching clean Mediaclass instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachDirty(Mediaclass instance) {
		log.debug("attaching dirty Mediaclass instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Mediaclass persistentInstance) {
		log.debug("deleting Mediaclass instance");
		try {
			super.delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Mediaclass instances");
		try {
			String queryString = "from Mediaclass";
			Query queryObject = getSession().createQuery(queryString);
			log.debug("size:::0" + queryObject.list().size());
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findAll(String str) {
		log.debug("finding all Mediaclass instances");
		try {
			String queryString = "from Mediaclass " + str;
			Query queryObject = getSession().createQuery(queryString);
			log.debug("size:::0" + queryObject.list().size());
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByAdduser(Object adduser) {
		return findByProperty(ADDUSER, adduser);
	}

	public List findByExample(Mediaclass instance) {
		log.debug("finding Mediaclass instance by example");
		try {
			List results = getSession().createCriteria("com.fick.hibernate.model.Mediaclass").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public Mediaclass findById(java.lang.String id) {
		log.debug("getting Mediaclass instance with id: " + id);
		try {
			Mediaclass instance = (Mediaclass) getSession().get("com.fick.hibernate.model.Mediaclass", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByMediaclassDescription(Object mediaclassDescription) {
		return findByProperty(MEDIACLASS_DESCRIPTION, mediaclassDescription);
	}

	public List findByMediaclassOrder(Object mediaclassOrder) {
		return findByProperty(MEDIACLASS_ORDER, mediaclassOrder);
	}

	public List findByMediaclassStatus(Object mediaclassStatus) {
		return findByProperty(MEDIACLASS_STATUS, mediaclassStatus);
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Mediaclass instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Mediaclass as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public Mediaclass merge(Mediaclass detachedInstance) {
		log.debug("merging Mediaclass instance");
		try {
			Mediaclass result = (Mediaclass) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void save(Mediaclass transientInstance) {
		log.debug("saving Mediaclass instance");
		try {
			super.save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
}